<!--
 * @Author: your name
 * @Date: 2020-12-03 17:46:12
 * @LastEditTime: 2021-05-28 17:30:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\inventory\record.vue
-->
<!--
 * @Author: your name
 * @Date: 2020-12-03 15:52:30
 * @LastEditTime: 2020-12-03 17:19:05
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\inventory\inventory_push.vue
-->
<template>
    <div>
        <el-dialog
        :title="$t('inventoryManage.inventoryRecord')"
        :visible="apiVisible"
        width="70%"
        @close="()=>{$store.commit('setApiVisible',false)}"
        :close-on-click-modal='true'
        >   
            <div class="record-mian-box">
                <div class="search-box">
                    <div class="search-box-sub">
                        <span class="lable-text">{{$t('index.timeHorizon')}}</span>
                        <el-date-picker
                        v-model="startTime"
                        type="date"
                        :placeholder="$t('selectDate')"
                        value-format='yyyy-MM-dd'
                        :clearable='true'
                        :picker-options="pickerOptionsStatus"
                        >
                        </el-date-picker>
                        -
                        <el-date-picker
                        v-model="endTime"
                        type="date"
                        :placeholder="$t('selectDate')"
                        value-format='yyyy-MM-dd'
                        :clearable='true'
                        :picker-options="pickerOptionsEnd"
                        >
                        </el-date-picker>
                    </div>
                    <div class="search-box-sub">
                        <el-button type="primary" @click='query'>{{$t('query')}}</el-button>
                    </div>
                    <div class="search-box-sub">
                        <el-button type="primary" @click='exportModal'>{{$t('export')}}</el-button>
                    </div>
                </div>
                <baseTable v-loading="loading"  row-key='sid' :data="dataRows" border  stripe :column='column' :select='true' @selection-change="handleSelectionChange($event)">
                    <template #numberR='{data}'>
                        {{returnNumber(data)}}
                    </template>
                    <template #status='{data}'>
                        {{returnStatus(data)}}
                    </template>
                    <template #storeType='{data}'>
                        {{returnType(data)}}
                    </template>
                    <template #option="{data}">
                        <div class="option-box">
                            <el-popconfirm
                                :confirm-button-text="$t('sure')"
                                :cancel-button-text="$t('cancel')"
                                :title="$t('confirmDelete')"
                                @confirm='deleteRow(data.sid)'
                                v-if='data.deleteFlag'
                            >
                                <el-button class="marginR10" type="" slot="reference" icon='el-icon-delete' :title='$t("delete")'></el-button>
                            </el-popconfirm>
                        </div>
                    </template>
                </baseTable>
                <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="current"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
                </el-pagination>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import baseTable from '../../components/common/baseTable'
export default {
    components: {
        baseTable
    },
    props: {
        returnStatusNumber:{
            type:Boolean,
        },
        editRow:{
            type:Object,
            default:()=>{return {}}
        }
    },
    data() {
        return {
            pickerOptions:{ //禁用今天之后的时间
                disabledDate(time) {
                    return time.getTime() > Date.now()
                }
            },
            startTime:'',
            endTime:'',
            loading:false,
            dataRows:[],
            column:[{
                title: this.$t('inventoryManage.object'),
                value: 'storeName',
                slot: 'storeName',
            },
            {
                title: this.$t('inventoryManage.type'),
                value: 'storeType',
                slot: 'storeType',
            },
            {
                title: this.$t('opttask.option'),
                value: 'status',
                slot: 'status',
            },
            {
                title: this.$t('inventoryManage.number'),
                value: 'numberR',
                slot: 'numberR',
                show:false
            },{
                title: this.$t('inventoryManage.operationTime'),
                value: 'operationTime',
                slot: 'operationTime',
            },
            {
                title: this.$t('opttask.option'),
                value: 'option',
                slot: 'option',
            }],
            size:10,
            current:1,
            total:1
        };
    },
    computed: {
        apiVisible(){
            return this.$store.state.sysStore.apiVisible
        },
        userType(){
            return localStorage.getItem('userType')
        },
        lan(){
            return localStorage.getItem('lan')
        },
        pickerOptionsStatus(){ //禁用今天之后的时间
        let that = this
            return {
                disabledDate(time) {
                    return time.getTime() > new Date(that.endTime).getTime() || time.getTime() > Date.now() 
                },
            }
        },
        pickerOptionsEnd(){ //禁用今天之后的时间
            let that = this
            return {
                disabledDate(time) {
                    return time.getTime() < new Date(that.startTime).getTime()-86400000 || time.getTime() > Date.now() 
                },
            }
            // that.searchData.startTime
        },
    },
    watch: {
        apiVisible:{
            handler(nv,ov){
                if(nv){
                    this.current = 1
                    this.size = 10
                    this.startTime = ''
                    this.endTime = ''
                    this.wmspartsrecordsGetPage()
                }
            }
        }
    },
    created() {

    },
    mounted() {
    },
    methods: {
        exportModal(){
            if(this.returnStatusNumber){
                // window.open('wmsGoodRecords/exportGoodsRecords.do?startTime='+this.startTime+"&endTime="+this.endTime+"&sn="+this.editRow.sn+'&lang='+localStorage.getItem('lang'))
                this.comm.windowOpen('/wmsGoodRecords/exportGoodsRecords?startTime='+this.startTime+"&endTime="+this.endTime+"&sn="+this.editRow.sn)
            }else{
                // window.open('wmsGoodRecords/exportGoodsRecords.do?startTime='+this.startTime+"&endTime="+this.endTime+"&partNo="+this.editRow.sn+"&id="+this.editRow.storeSid+'&lang='+localStorage.getItem('lang'))
                this.comm.windowOpen('/wmsGoodRecords/exportGoodsRecords?startTime='+this.startTime+"&endTime="+this.endTime+"&partNo="+this.editRow.sn+"&id="+this.editRow.storeSid)
            }
        },
        returnType(row){
            var typeMsg = ''
            if(row.storeType == 2){
                typeMsg = this.$t('firm')
            }
            if(row.storeType == 3){
                typeMsg = this.$t('agent')
            }
            if(row.storeType == 4){
                typeMsg = this.$t('installer')
            }
            return typeMsg
        },
        returnStatus(row){
            var statusMsg = ''
            if(row.status == 1){
                statusMsg = this.$t('inventoryManage.push')
            }
            if(row.status == 2){
                statusMsg = this.$t('inventoryManage.recordPullAll')
            }
            if(row.status == 3){
                statusMsg = this.$t('inventoryManage.recordPull')
            }
            if(row.status == 4){
                statusMsg = this.$t('inventoryManage.recordInstall')
            }
            if(row.status == 5){
                statusMsg = this.$t('inventoryManage.stockStatus')
            }
            return statusMsg
        },
        async deleteRow(id){
            let res = ''
            if(this.returnStatusNumber){
                res = await this.API.wmsGoodRecordsDelete({
                    id
                })
            }else{
                res = await this.API.wmspartsrecordsDelete({
                    id
                })
            }
            if(!res.success){
                this.comm.showMsg(false,this.$t('index.control_fail'))
            }
            if(res.object == 0){
                this.comm.showMsg(true,this.$t('index.control_success'))
            }
            if(res.object == 1){
                this.comm.showMsg(false,this.$t('inventoryManage.objectNo'))
            }
            if(res.object == 2){
                this.comm.showMsg(false,this.$t('inventoryManage.objectstock'))
            }
            if(res.object == 3){
                this.comm.showMsg(false,this.$t('index.control_fail'))
            }
            this.wmspartsrecordsGetPage()
            this.$emit('uploadData',true)
            // $('#subTable').bootstrapTable('refresh');
            // $('#dataTable').bootstrapTable('refresh');
        },
        returnNumber(row){
            if(row.status == 1 || row.status == 4){
                return row.sendNum
            }else{
                return row.recieveNum
            }
        },
        async wmspartsrecordsGetPage(){

            let obj = {
                partNo:this.editRow.sn,
                endTime:this.endTime,
                id:this.editRow.storeSid,
                startTime:this.startTime,
                size:this.size,
                current:this.current
            }
            let obj2 = {
                sn: this.editRow.sn,
                size: this.size,
                current: this.current,
                startTime: this.startTime,
                endTime:this.endTime,

            }
            let obj3 = {}
            let res = ''
            this.loading = true
            if(this.returnStatusNumber){
                this.column[3].show = false
                obj3 = obj2
                res = await this.API.wmsGoodRecordsGetPage(obj3)
            }else{
                this.column[3].show = true
                obj3 = obj
                res = await this.API.wmspartsrecordsGetPage(obj3)
            }
            this.total = res.total
            this.dataRows = res.rows || []
            this.loading = false
        },
        query(){
            this.current = 1
            this.wmspartsrecordsGetPage()
        },
        downloadModal(){
            window.open('wmsstock/downloadTemplate.do?lang='+this.lan)
        },
        
        handleSizeChange(val) {
          this.size = val
          this.current = 1
          this.wmsstockGetPage()
        },
        handleCurrentChange(val) { 
          this.current = val
          this.wmsstockGetPage()
        },
        handleSelectionChange(val){
          this.selectDataList=val
        },
    },
};
</script>

<style  lang="less" scoped>
.record-mian-box{
    padding-bottom:50px; 
}
</style>
